<%@ page contentType="text/html;charset=UTF-8"%>

<div id="rap-page-area-asset" class="rap-page-area hide">
<div class="tabtable" >
	<ul id="myTab" class="nav nav-tabs">
		<li class="active">
			<a href="#subTable" id="subTable-tab" data-toggle="tab">
				承租资产列表
			</a>
		</li>
	</ul>
	<div class="tab-content">
		<div class="tab-pane active">
			<div class="rap-toolbar" id="rap-toolbar-assetlist">
				<button id="rap-btn-add-asset">
					<i class="fa fa-plus"></i> 添加承租资产
				</button>
				<button id="rap-btn-remove-asset">
					<i class="fa fa-trash-o"></i> 删除承租资产
				</button>
			</div>
			<div class="">
				<table id="rap-table-list-asset" class="table rap-table rap-table-list">
				<thead>
					<tr>
						<th><input type="checkbox" /> <span class="lbl"></span></th>
						<th>资产名称</th>
						<th>所属物业</th>
						<th>资产面积(㎡)</th>
						<th>区域</th>
						<th>用途</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
				</tbody>
			</table>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
var _assetTableList;
var _contractId ;
var _assetTableCols = [{
	data : "uuid",
	sortable : false,
	className : "center",
	width : "30",
	render : RapDataTableUtils.renderCheckCol
},
{
	data : 'asset.theName',
	width : "120",
	render : function(data, type, full) {
		return RapDataTableUtils.renderStrByLength(data,24);
	},
	sortable : true
},
{
	data : null,
	width : "120",
	render : function(data, type, full) {
		var content = data.asset.property==null?"":data.asset.property.theName;
		return RapDataTableUtils.renderStrByLength(content,24);
	},
	sortable : true
},
{
	data : 'asset.area',
	width : "120",
	className :"rap-td-number",
	render:milliFormat,
	sortable : true
},
{
	data : 'asset.region',
	width : "120",
	sortable : true,
	render : function(data, type, full) {
		return RapDataUtils.getEnumName("stwy_region", data);
	}
},
{
	data : 'asset.usefor',
	width : "120",
	sortable : true,
	render : function(data, type, full) {
		return RapDataUtils.getEnumName("stwy_usefor", data);
	}
},
{
	data : "uuid",
	className : "center",
	sortable : false,
	render : renderAssetActionCol,
	width : "80"
}]
;
function renderAssetActionCol(){
	return "<div class='rap-btn-actiongroup'>" 
	+ "<button id='rap-btn-remove-row' class='btn btn-xs btn-danger' data-rel='tooltip' title='删除'><i class='fa fa-trash-o'></i></button>"
	+ "</div>";
}
function setAssetActions(nRow, aData, iDataIndex) {
	$('#rap-btn-remove-row', nRow).click(function() {
		removeAsset(aData, iDataIndex, nRow);
	});

}
function removeAsset(aData, iDataIndex, nRow){
	
	_assetTableList.row(nRow).remove().draw(false);
	/* RapDataUtils.removeRecord('${ctx}/lease/contractasset/delete', [ aData.uuid ],
			function() {
				_assetTableList.row(nRow).remove().draw(false);
			}); */
	calTotalArea();
}
function onRemoveAssets(){
	/* var pks = RapDataTableUtils.getSelectPks("tableList");
	var url = '${ctx}/lease/contractasset/delete';
	RapDataUtils.removeRecord(url, pks, loadAssetList); */
	var pks = RapDataTableUtils.getSelectPks("rap-table-list-asset");
	if (pks.length < 1){
		alert("请选择需要删除的记录");
		return;
	}
	var rows = RapDataTableUtils.getSelectRows(_assetTableList);
	_assetTableList.rows(rows).remove().draw(false);
	
	calTotalArea();
}
function showAssetList(showType){
	$("#rap-page-area-asset").removeClass("hide");
	if("view"==showType){
		$("#rap-toolbar-assetlist").addClass("hide");
		$('#rap-table-list-asset tr th:eq(6)').hide();  
		
	}else{
		$("#rap-toolbar-assetlist").removeClass("hide");
		$('#rap-table-list-asset tr th:eq(6)').show(); 
	}
}
function hideAssetList(){
	$("#rap-page-area-asset").addClass("hide");
}

function loadAssetList(showType) {
	$.ajax({
		url : '${ctx}/lease/contractasset/query',
		data : {"search_EQ_contract.uuid":_contractId},
		dataType : 'json',
		success : function(data) {
			_assetTableList.clear();
			_assetTableList.rows.add(data.records);
			_assetTableList.draw();
			if("view"==showType){
				$('#rap-table-list-asset tr').find('td:eq(6)').hide();  
			}else{
				$('#rap-table-list-asset tr').find('td:eq(6)').show(); 
			}
		},
		error :function(){
			alert("服务器请求失败，请重新登录");
		}
	});
}
function calTotalArea(){
	var area = 0;
	var rows = _assetTableList.rows()[0];
	for(var i=0;i<rows.length;i++){
		var data = _assetTableList.row(rows[i]).data();
		area = area + data.asset.area;
	}
	$("#rap-form-edit input[name='area']").val(area);
	calculatePrice();
}
function callbackAsset(data){
	var contractAssetList = new Array();
	var rows = _assetTableList.rows()[0];
	for(var i = 0 ;i<data.length;i++){
		var asset = data[i];
		var hasIn = false;
		for(var j=0;j<rows.length;j++){
			var rowData = _assetTableList.row(rows[j]).data();
			if(rowData.asset.uuid==asset.uuid){
				hasIn=true;
				break;
			}
		}
		if(!hasIn){
			var contractAsset = {"contract":{"uuid":""},"asset":asset};
			contractAssetList.push(contractAsset);
		}
	}
	if(contractAssetList.length>0){
		_assetTableList.rows.add(contractAssetList);
		_assetTableList.draw();
		//合同总面积计算
		calTotalArea();
		
		//设置组别，采用第一个asset的组别
		$("input[name='groupOne']").val(contractAssetList[0].asset.groupOne);
		$("input[name='groupTwo']").val(contractAssetList[0].asset.groupTwo);
	}
}
$(document).ready(function() {
	$("#rap-btn-remove-asset").bind("click",function(){
		onRemoveAssets();
	});
	_assetTableList = $('#rap-table-list-asset').DataTable({
		"columns" : _assetTableCols,
		"createdRow" : setAssetActions,
		"order": [],
		"paging" : false
	});
	RapDataTableUtils.setCheckCol(_assetTableList);
	$("#rap-btn-add-asset").colorbox({
		iframe : true,
		opacity	: 0.2,
		transition : 'none',
		speed : 200,
		href : "${ctx}/pro/asset/select?search_EQ_status=1&search_EQ_assetState=0&callback=callbackAsset&isMulti=true",
		innerWidth : '90%',
		innerHeight : '80%',
		close : '×',
		fixed : true
	});
});
</script>